package com.doraro.platform.module.device.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.doraro.platform.common.beans.params.PageParam;
import com.doraro.platform.common.beans.response.PageView;
import com.doraro.platform.common.utils.Result;
import com.doraro.platform.common.utils.ShiroUserUtil;
import com.doraro.platform.module.device.model.entity.RepairAdvice;
import com.doraro.platform.module.device.model.entity.RepairOrder;
import com.doraro.platform.module.device.model.param.RepairAdviceParam;
import com.doraro.platform.module.device.model.param.cond.AdviceCond;
import com.doraro.platform.module.device.service.IRepairAdviceService;
import com.doraro.platform.module.device.service.IRepairOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author doraro
 * @since 2019-04-21
 */
@RestController
@RequestMapping("/api/advice")
@Api(description = "维修单建议相关接口", tags = "advice")
public class RepairAdviceRestController {
    private final IRepairOrderService orderService;
    private final IRepairAdviceService adviceService;

    @Autowired
    public RepairAdviceRestController(IRepairOrderService orderService, IRepairAdviceService adviceService) {
        this.orderService = orderService;
        this.adviceService = adviceService;
    }


    @GetMapping("user/view")
    @ApiOperation("用户根据维修单id,查看故障设备解决方案")
    @RequiresPermissions("advice:order:view")
    public Result viewAdviceByOrderId(@RequestParam String orderId) {
        final String userId = ShiroUserUtil.getCurrentId();
        final RepairOrder order = orderService.getUnresolvedOrder(orderId, userId);
        final List<RepairAdvice> list = adviceService.lambdaQuery()
                .eq(RepairAdvice::getOrderId, orderId)
                .list();
        return new Result().ok().addData("order", order).addData("advice", list);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取解决方案详情")
    @RequiresPermissions("advice:view")
    public Result getDetail(@PathVariable Integer id) {
        final RepairAdvice advice = adviceService.getById(id);
        return new Result().ok().addData("advice", advice);
    }

    @GetMapping("/page")
    @ApiOperation("工程师分页获取解决方案")
    @RequiresPermissions("advice:page")
    public Result getDetail(PageParam<RepairAdvice> pageParam, AdviceCond cond) {
        final LambdaQueryWrapper<RepairAdvice> wrapper =
                cond.condition()
                        .eq(RepairAdvice::getEngineerId, ShiroUserUtil.getCurrentId());
        final IPage<RepairAdvice> page = adviceService.page(pageParam.toPage(), wrapper);
        return new Result().ok().addData("page", new PageView(page));
    }
    @PostMapping("/submit")
    @ApiOperation("工程师提交或修改故障设备解决方案")
    @RequiresPermissions("advice:submit")
    public Result advice(@Validated @RequestBody RepairAdviceParam solution) {
        adviceService.saveOrUpdateAdvice(ShiroUserUtil.getCurrentId(), solution);
        return new Result().ok();
    }


    @PostMapping("/choice")
    @ApiOperation("用户根据解决方案id，维修单id选择解决方案")
    @RequiresPermissions("advice:choice")
    public Result choiceRepairAdvice(@RequestParam String orderId, @RequestParam Integer adviceId) {
        adviceService.choiceAdvice(orderId, adviceId, ShiroUserUtil.getCurrentId());
        return new Result().ok();
    }


}

